from __future__ import annotations

from sqlalchemy.ext.asyncio import AsyncSession

from app.models.relationship import Relationship
from app.models.user import User


async def get_partner_email(session: AsyncSession, user: User) -> str | None:
  if not user.active_relationship_id:
    return None
  relationship = await session.get(Relationship, user.active_relationship_id)
  if not relationship:
    return None
  partner_id = (
    relationship.partner_id if relationship.initiator_id == user.id else relationship.initiator_id
  )
  if not partner_id:
    return None
  partner = await session.get(User, partner_id)
  if not partner:
    return None
  return partner.email
